//
//  LWStatusToolBar.swift
//  LW_Weibo
//
//  Created by 刘伟 on 16/7/13.
//  Copyright © 2016年 LWSpro. All rights reserved.
//

import UIKit

class LWStatusToolBar: UIView {

    //添加属性  为了给内容赋值
    var statusViewModel: LWStatusViewModel? {
        
        //set方法 重写  只是不需要写 self.statusViewModel = statusViewModel
        didSet {
            
            //在这里面给子控件赋值  这里拿到statusViewModel中的三个属性 也就拿到了逻辑判断 逻辑判断就包含在了三个属性中
            //三个属性是有值的  因为外界传进来的statusViewModel 是有值的
            retweetButton.setTitle(statusViewModel?.retweetStr, forState: .Normal)
            
            commentButton.setTitle(statusViewModel?.commentStr, forState: .Normal)
            
            unlikeButton.setTitle(statusViewModel?.unlikeStr, forState: .Normal)
            
            
            
        }
        
        
        
    }
    

    
    //MARK: - 懒加载
    //转发
    private lazy var retweetButton: UIButton = self.setupUI("timeline_icon_retweet", title: "转发")
    
    //评论
    private lazy var commentButton: UIButton = self.setupUI("timeline_icon_comment", title: "评论")
    
    
    //赞
    private lazy var unlikeButton: UIButton = self.setupUI("timeline_icon_unlike", title: "赞")
    
    
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        layoutUI()
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //MARK: - 约束
    private func layoutUI() {
        
        retweetButton.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.bottom.equalTo(self)
            make.trailing.equalTo(commentButton.snp_leading)
            
            make.width.equalTo(commentButton)
            
        }
        
        commentButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(retweetButton)
            
            make.trailing.equalTo(unlikeButton.snp_leading)
            
            make.width.equalTo(unlikeButton)
            
        }
        
        
        unlikeButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(retweetButton)
            
            make.trailing.equalTo(self)
            
            
        }
        
        
        
        //添加竖线  在button后面添加  不然会被button挡住
        let firstLine = addChildLine()
        let secondLine = addChildLine()
        
        //约束
        firstLine.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(self.snp_centerY)
            
            make.centerX.equalTo(retweetButton.snp_trailing)
            
            
        }
        
        
        secondLine.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(commentButton.snp_trailing)
            
            make.centerY.equalTo(self.snp_centerY)
            
            
        }
        
        
        
    }
    
    //MARK: - 按钮的逻辑判断
    
    //要对三个按钮进行逻辑判断  转发/评论/赞 数目 为0时,则显示文字 有数目时显示数字 如果数目大于10000那么显示多少万
    //因为三个按钮的数据是通过网络获取的  所以逻辑判断我们放在负责cell的viewModel中进行,那里面能拿到网络数据  这样view中的代码就简答清晰多了
    
    
    
    //MARK: - 竖线
    private func addChildLine() -> UIImageView {
        
        let lineImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        
        
        //添加子控件
        addSubview(lineImageView)
        
        
        return lineImageView
        
        
    }
    
    
    //MARK: - 创建button
    private func setupUI(imageName: String, title: String) -> UIButton {
        
        
        let button = UIButton()
        
        //设置图片
        button.setImage(UIImage(named: imageName), forState: .Normal)
        
        //设置文字
        button.setTitle(title, forState: .Normal)
        //指定文字大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //设置文字颜色
        button.setTitleColor(.grayColor(), forState: .Normal)
        
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        
        //取消高亮
        button.adjustsImageWhenHighlighted = false
        
        
        //添加按钮
        addSubview(button)
        
        return button
        
    }
    

    
    
    
    
    
    
    
    
}
